package main

import (
	"log"
	"net"
	"time"
)

func handleConnection(conn net.Conn) {
	defer conn.Close()
	var buffer []byte = []byte("You are welcom. I'm server.")
	for {
		time.Sleep(3 * time.Second) // sleep 3s
		n, err := conn.Write(buffer)
		if err != nil {
			log.Println("Write error: ", err)
			break
		}
		log.Println("send: ", n)
	}
	log.Println("connection end")
}

func main() {
	addr := "0.0.0.0:9090"
	tcpAddr, err := net.ResolveTCPAddr("tcp", addr)

	if err != nil {
		log.Fatalf("net.ResovleTCPAddr fail:%s", addr)
	}

	listener, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		log.Fatalf("listen %s fail: %s", addr, err)
	} else {
		log.Println("listening", addr)
	}

	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Println("listener.Accept error: ", err)
			continue
		}

		go handleConnection(conn)
	}

}
